package com.example.permission.myjbm.activitys;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.permission.myjbm.config.Config;
import com.example.permission.myjbm.dao.BaseAty;
import com.example.permission.myjbm.model.IntegralDetailModel;
import com.example.permission.myjbm.net.ReleaseNet;
import com.example.permission.myjbm.utils.ScreenAsset;
import com.jbm.jbmsupplier.util.ExProductEntity;
import com.jbm.jbmsupplier.util.ICtrl;
import com.jbm.jbmsupplier.util.VoucherEntity;
import com.jbm.jbmsupplier.util.enums.ExChangeType;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 兑换详情
 * 分为详情和兑换
 * isDetial    1详情    0兑换
 */

public class IntegralDetail extends BaseAty {
    //返回
    @InjectView(R.id.header_back)
    ImageView headerBack;
    //标题
    @InjectView(R.id.header_title)
    TextView headerTitle;
    //提交
    @InjectView(R.id.header_right)
    TextView headerRight;
    //优惠图片
    @InjectView(R.id.integral_detail_img)
    ImageView integralDetailImg;
    //内容标题
    @InjectView(R.id.integral_detail_title)
    TextView integralDetailTitle;
    //内容积分
    @InjectView(R.id.integral_detail_price)
    TextView integralDetailPrice;
    //其次钱
    @InjectView(R.id.integral_detail_secondprice)
    TextView integralDetailSecondprice;
    //详细内容
    @InjectView(R.id.integral_detail_content)
    TextView integralDetailContent;
    //兑换
    @InjectView(R.id.integral_detail_duihuan)
    TextView integralDetailDuihuan;
    //二维码
    @InjectView(R.id.integral_detail_erweima)
    ImageView integralDetailErweima;

    private IntegralDetailModel model;
    private ReleaseNet net;
    private int exp_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_detail);
        ButterKnife.inject(this);
        initView();
    }

    private void initView() {
        int pd = getIntent().getIntExtra("isDetial", 0);
        //商品ID
        exp_id = getIntent().getIntExtra("exp_id", 0);
        net = new ReleaseNet(handlerAty);
        baseLoader.show();
        switch (pd) {
            case 0:
                //代表兑换
                model = new IntegralDetailModel(headerRight, headerTitle, "优惠券兑换", null);
                net.getMechantDetail(exp_id);
                break;
            case 1:
                //代表展示详情
                integralDetailDuihuan.setVisibility(View.GONE);
//                integralDetailContent.setVisibility(View.GONE);
                integralDetailPrice.setVisibility(View.GONE);
                model = new IntegralDetailModel(headerRight, headerTitle, "优惠券详情", null);
                net.getDJDetial(exp_id);
                break;
        }
    }

    @Override
    protected void netHandler(int what, Object obj) {
        super.netHandler(what, obj);
        SpannableString string;
        switch (what) {
            //兑换
            case ReleaseNet.MECHANTDETAILERROR:
                ExProductEntity ob = (ExProductEntity) obj;
                Picasso.with(IntegralDetail.this).load(Config.PHOTOPATH+ob.getPicpath()).into(integralDetailImg);
                integralDetailTitle.setText(ob.getName());
                 string=new SpannableString(ob.getAmount() + "积分");
                string.setSpan(new AbsoluteSizeSpan(30),string.length()-2,string.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                integralDetailPrice.setText(string);
                string=new SpannableString("￥"+ob.getSum());
                string.setSpan(new StrikethroughSpan(),0,string.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                integralDetailSecondprice.setText(string);

                break;
            //详情
            case ReleaseNet.DAIJINDETAILERROR:
                VoucherEntity ob1 = (VoucherEntity) obj;
                Picasso.with(IntegralDetail.this).load(Config.PHOTOPATH+ob1.getPicpath()).into(integralDetailImg);
                integralDetailTitle.setText(ob1.getMemo());
            //    integralDetailPrice.setText(ob1.getAmount() + "积分");
                 string=new SpannableString("￥"+ob1.getAmount());
                string.setSpan(new AbsoluteSizeSpan(30),0,string.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                string.setSpan(new ForegroundColorSpan(Color.RED),0,string.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
               // integralDetailPrice.setText(string);
                integralDetailSecondprice.setText(string);
                integralDetailErweima.setImageBitmap(model.generateBitmap(ob1.getVoucherCode(), ScreenAsset.getScreenWidth(IntegralDetail.this)/2,ScreenAsset.getScreenWidth(IntegralDetail.this)/2));

                break;
            //兑换
            case ReleaseNet.DUIHUANERROR:
                if(((ICtrl)obj).back){
                    setResult(RESULT_OK);
                    finish();
                }else {
                    hint(((ICtrl) obj).errorinfo);
                }
                break;

        }


    }

    @OnClick({R.id.header_back, R.id.integral_detail_duihuan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.header_back:
                finish();
                break;
            case R.id.integral_detail_duihuan:
                //兑换操作
                baseLoader.show();
                net.duiHuan(exp_id, ExChangeType.voucher.toValue());
                break;
        }
    }


}
